package com.two.shopping.controller;

import com.two.shopping.pojo.Commodity;
import com.two.shopping.pojo.PageBean;
import com.two.shopping.pojo.Result;
import com.two.shopping.service.IShoppingCenterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/*这是商城模块的控制层，也就是商城的Controller层
用于接收前端的get,post等请求接受并交给服务层
该层具有的方法如下{
”idList(@PathVariable Integer id) “ :"单次查询商品"
” page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "2") Integer pageSize, String name) “:"分页查询所有商品"
}

* */
@Slf4j
@RequestMapping("/commodity")
@RestController
public class ShoppingCentreController {
    @Autowired
    IShoppingCenterService shoppingCenterService;
    @CrossOrigin
    //根据ID单次查询
    @GetMapping("/{id}")
    private Result idList(@PathVariable Integer id) {
        log.info("进行了一次商品查询，查询的商品id为{}", id);
        Commodity commodity = shoppingCenterService.idList(id);
        return Result.success(commodity);
    }
    @CrossOrigin
    //全部查询
    @GetMapping("/list")
    public Result pageName(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "2") Integer pageSize) {
        log.info("分页查询所有信息，参数为{}，{}", page, pageSize);
        PageBean pageBean = shoppingCenterService.pageAll(page, pageSize);
        return Result.success(pageBean);
    }
    @CrossOrigin
    //根据类型查询
    @GetMapping("/list/type")
    public Result pageType(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "2") Integer pageSize,
                           Integer comodeityTypeId) {
        log.info("分页查询所有信息,参数为{},{},{}", page, pageSize, comodeityTypeId);
        PageBean pageBean = shoppingCenterService.pageType(page, pageSize, comodeityTypeId);
        return Result.success(pageBean);
    }
    @CrossOrigin
    //模糊查询name,且可以根据价格范围进行查询
    @GetMapping("/listLike")
    public Result pageLike(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           String name,
                           @RequestParam(defaultValue = "0")Float priceBegin,
                           @RequestParam(defaultValue = "100000")Float priceEnd) {
        log.info("分页查询所有信息，参数为，{},{},{},{},{}", page, pageSize, name, priceBegin
                , priceEnd);

        PageBean pageBean = shoppingCenterService.pageLike(page, pageSize, name, priceBegin, priceEnd);
        return Result.success(pageBean);

    }
    @CrossOrigin
    @GetMapping("/listId")
    public Result selectCommodityId(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    Integer commodityId) {
        log.info("查询商品类型编号为{}", commodityId);
        PageBean pageBean = shoppingCenterService.selectCommodityId(page, pageSize, commodityId);
        return Result.success(pageBean);
    }
}